package cn.edu.cqu.fredyvia.forum.service.impl;

import cn.edu.cqu.fredyvia.forum.Utils.FileType;
import cn.edu.cqu.fredyvia.forum.Utils.Utils;
import cn.edu.cqu.fredyvia.forum.config.AliyunOssConfig;
import cn.edu.cqu.fredyvia.forum.entity.User;
import cn.edu.cqu.fredyvia.forum.entity.UserInfo;
import cn.edu.cqu.fredyvia.forum.mapper.UserInfoMapper;
import cn.edu.cqu.fredyvia.forum.mapper.UserMapper;
import cn.edu.cqu.fredyvia.forum.service.IUserInfoService;
import cn.edu.cqu.fredyvia.forum.storage.service.impl.AliyunOssStorageProviderImpl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.net.URL;
import java.nio.file.Paths;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author fredyvia
 * @since 2021-07-10
 */
@Service
@Slf4j
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements IUserInfoService {


    @Autowired
    AliyunOssStorageProviderImpl aliyunOssStorageProvider;

    @Autowired
    UserInfoMapper userInfoMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    AliyunOssConfig aliyunOssConfig;

    public String upload(User user, MultipartFile file) {
        //if(file==null)return "头像不能为空";
        // System.out.println(fileName);
        try {
            log.info(file.getOriginalFilename());
            log.info(Utils.getSuffix(file.getOriginalFilename()));
            FileType fileType = FileType.getFileType(Utils.getSuffix(file.getOriginalFilename()));
            UserInfo userInfo = userInfoMapper.selectById(user.getId());
            String fileName = aliyunOssConfig.getImageBasepath()+"/"+Utils.getRandomFileName(fileType.toString());
//            String olderAvatar = userInfo.getAvatar();
//            if (!Utils.isBlank(olderAvatar)){
//
//            }
            String newAvatar = "https://" + aliyunOssConfig.getBucketname() + "." + aliyunOssConfig.getEndpoint() + "/" +fileName;
            userInfo.setAvatar(newAvatar);
            new UploadThread(file, fileName,userInfo).start();
            return newAvatar;
        } catch (Exception e) {
            log.error("error:",e);
            return null;
        }
    }

    class UploadThread extends Thread {
        MultipartFile multiFile;
        String fileName;
        UserInfo userInfo;

        UploadThread(MultipartFile multiFile, String fileName,UserInfo userinfo) {
            this.multiFile = multiFile;
            this.fileName = fileName;
            this.userInfo = userinfo;
        }

        @Override
        public void run() {
            try {
                // 这里一定是先上传成功了再更新数据库
                String olderAvatar = userInfo.getAvatar();
                aliyunOssStorageProvider.save(multiFile.getInputStream(), Paths.get("images"+"/"+fileName));
                userInfoMapper.updateById(userInfo);
                if (!Utils.isBlank(olderAvatar)){
                    URL url = new URL(olderAvatar);
                    aliyunOssStorageProvider.remove(Paths.get(url.getPath()));
                }
            } catch (Exception e) {
                log.error("error:",e);
            }
        }
    }
}
